import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import createVitePlugins from './vite/plugins'
// https://vitejs.dev/config/ ruoyi的环境配置

export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_ENV } = env
  const { VITE_APP_BASE_API } = env
  const { VITE_APP_BASE_URL } = env
  return {
    plugins: [createVitePlugins(env, command === 'build')],
    base: VITE_APP_ENV === 'production' ? '/' : '/',
    resolve: {
      alias: {
        // 设置路径
        '~': path.resolve(__dirname, './'),
        // 设置别名
        '@': path.resolve(__dirname, './src')
      },
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    // vite 相关配置 https://cn.vitejs.dev/config/#server-proxy
    server: {
      port: 3000,
      cors: true,
      host: true,
      open: true,
      proxy: {
        [VITE_APP_BASE_API]: {
          // 要用localhost就改下注释，别把下面网址改了，懒得修改
          // target: 'http://localhost:8080',
          target: VITE_APP_BASE_URL,
          changeOrigin: true,
          rewrite: (p) => p.replace(RegExp(`^${env.VITE_APP_BASE_API}`), '')
        }
      }
    }
  }
})
